﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.OA.Email
{
    public partial class email_space_list : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ViewState["collection"] = (null != Request.QueryString["collection"]) ? Request.QueryString["collection"].ToString() : "";
                ViewState["collectionremain"] = (null != Request.QueryString["collectionremain"]) ? Request.QueryString["collectionremain"].ToString() : "";
                InitPage();
            }
            BindGridView(ViewState["collection"].ToString().Trim(), ViewState["collectionremain"].ToString());
        }

        private void InitPage()
        {
            lbSet.Attributes["onclick"] = "javascript:return doSet();";
            lbAllSet.Attributes["onclick"] = "javascript:return doSetAll();";
            txtSetSpace.Attributes.Add("onkeyup", "CliNumber('txtSetSpace');");
        }

        protected void grid_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.ASPxGridView.GridViewRowType.Data) return;
            string sUSERID = grid.GetRowValues(e.VisibleIndex, "userid").ToString().Trim();
            double sPACESIZE =double.Parse(grid.GetRowValues(e.VisibleIndex, "spacesize").ToString().Trim());
            double sYYSPACE = double.Parse(grid.GetRowValues(e.VisibleIndex, "yyspace").ToString().Trim());
            e.Row.Cells[4].Text = sPACESIZE.ToString("f2") + "(M)";
            e.Row.Cells[5].Text = sYYSPACE.ToString("f2") + "(M)";
            e.Row.Cells[6].Text = (sPACESIZE - sYYSPACE).ToString("f2") + "(M)";

        }

        private DataSet GetDateBind(string Coillar, string collectionremain)
        {
            EmailBLL bll = new EmailBLL();
            DataSet dSet = bll.GetSpaceData(Coillar, collectionremain);
            bll.Release();
            bll = null;
            return dSet;
        }

        protected void BindGridView(string Coillar,string collectionremain)
        {
            grid.DataSource = GetDateBind(Coillar, collectionremain);
            grid.DataBind();
        }

        protected void lbSel_Click(object sender, EventArgs e)
        {
            string collection = "";
            string collectionremain = "";
            string stxtKeyword = txtKeyword.Value.Trim();    //关键字
            string stxtEmlsign = txtEmlsign.Text.Trim();    //查询条件 邮件空间
            string stxtyet = txtyet.Text.Trim();            //查询条件 已使用空间

            if (stxtKeyword != "")
            {
                collection = "( v_udp.username like '%" + stxtKeyword + "%' or v_udp.deptname like '%" + stxtKeyword + "%' or v_udp.postionname like '%" + stxtKeyword + "%') ";
            }

            if (stxtEmlsign != "")
            {
                if (collection != "")
                {
                    collection += " and";
                }
                collection += " (EML_A7.EML_A7_20 " + ddEmlsign.SelectedItem.Text.Trim() + " " +stxtEmlsign + ")";
            }
            if (collection != "")
            {
                collection = " where " + collection;
            }
            if (stxtyet != "")
            {
                collectionremain = " group by att_a2_80 HAVING sum(att_a2_80)  " + ddyet.SelectedItem.Text.Trim() + " " + stxtyet;
            }
            ViewState["collection"] = collection;
            ViewState["collectionremain"] = collectionremain;
            BindGridView(collection, collectionremain);
        }

        protected void btSet_Click(object sender, EventArgs e)
        {
            string collection = ViewState["collection"].ToString().Trim();
            string collectionremain = ViewState["collectionremain"].ToString().Trim();
            bool bFlag = false;
            MsEmailSpace msSpace = new MsEmailSpace();
            EmailBLL bll = new EmailBLL();
            msSpace.userId = hidIds.Value;
            msSpace.space = txtSetSpace.Value.Trim();

            bFlag = bll.UpdateSpace(msSpace);
            if (bFlag == true)
            {
                BindGridView(collection, collectionremain);
            }
            else
            {
                Response.Write("<script>设置失败!</script>");
            }

            txtSetSpace.Value = "";
        }

    }
}
